<?php
class ModelRestaurantReview extends Model {		
	public function addReview($restaurant_id, $data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "restaurant_review SET author = '" . $this->db->escape($data['name']) . "', customer_id = '" . (int)$this->customer->getId() . "', restaurant_id = '" . (int)$restaurant_id . "', text = '" . $this->db->escape(strip_tags($data['text'])) . "', rating = '" . (int)$data['rating'] . "', date_added = NOW()");
	}
		
	public function getReviewsByRestaurantId($restaurant_id, $start = 0, $limit = 20) {
		$query = $this->db->query("SELECT rv.review_id, rv.author, rv.rating, rv.text, r.restaurant_id, rd.name, r.image, rv.date_added FROM " . DB_PREFIX . "restaurant_review rv LEFT JOIN " . DB_PREFIX . "restaurant r ON (rv.restaurant_id = r.restaurant_id) LEFT JOIN " . DB_PREFIX . "restaurant_description rd ON (r.restaurant_id = rd.restaurant_id) WHERE r.restaurant_id = '" . (int)$restaurant_id . "' AND rv.status = '1' AND r.status = '1' AND rd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY r.date_added DESC LIMIT " . (int)$start . "," . (int)$limit);
		
		return $query->rows;
	}
	
	public function getAverageRating($restaurant_id) {
		$query = $this->db->query("SELECT AVG(rating) AS total FROM " . DB_PREFIX . "restaurant_review WHERE status = '1' AND restaurant_id = '" . (int)$restaurant_id . "' GROUP BY restaurant_id");
		
		if (isset($query->row['total'])) {
			return (int)$query->row['total'];
		} else {
			return 0;
		}
	}
	
	public function getTotalReviews() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "restaurant_review rv LEFT JOIN " . DB_PREFIX . "restaurant r ON (rv.restaurant_id = r.restaurant_id) WHERE rv.status = '1' AND r.status = '1'");
		
		return $query->row['total'];
	}

	public function getTotalReviewsByRestaurantId($restaurant_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "restaurant_review rv LEFT JOIN " . DB_PREFIX . "restaurant r ON (rv.restaurant_id = r.restaurant_id) LEFT JOIN " . DB_PREFIX . "restaurant_description rd ON (r.restaurant_id = rd.restaurant_id) WHERE rv.restaurant_id = '" . (int)$restaurant_id . "' AND rv.status = '1' AND r.status = '1' AND rd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
		
		return $query->row['total'];
	}
}
?>